package com.cdc.image;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

/**
 * Created by cdc on 2018/3/27.
 */

public class BitmapUtil {

    public static Bitmap ratio(String filePath,int pixelW,int pixelH){
        BitmapFactory.Options newOptions=new BitmapFactory.Options();
        //只加载图片的长宽  不加载图片的实际内容
        newOptions.inJustDecodeBounds=true;
        newOptions.inPreferredConfig=Bitmap.Config.RGB_565;
        //预加载
        BitmapFactory.decodeFile(filePath,newOptions);
        int originalW=newOptions.outWidth;
        int originalH=newOptions.outHeight;
        newOptions.inSampleSize=getSimpleSize(originalW,originalH,pixelW,pixelH);
        newOptions.inJustDecodeBounds=false;
        return BitmapFactory.decodeFile(filePath,newOptions);
    }

    private static int getSimpleSize(int originalW, int originalH, int pixelW, int pixelH) {
        int simpleSize=1;
        if(originalW> originalH && originalW > pixelW){
            simpleSize=originalW/pixelW;
        }else if(originalW < originalH &&  originalH > pixelH){
            simpleSize=originalH/pixelH;
        }
        if(simpleSize<=0){
            simpleSize=1;
        }
        return simpleSize;
    }
}
